MySQL - First access in ASP.Net

Okay, try using our ASP.NET to reach our test table for the first time. First of all, let us system. The data must be imported into the namespace namespace. Add the following line to the top of your CodeBehind file:


using System.Data.Odbc;

This will give us access to a group of sections belonging to Odyssey. For now, we'll need 3 of them:

OdbcConnection - Provides an ODBC connection to a database.
OdbcCommand - Will execute an SQL command using an existing OdbcConnection.
OdbcDataReader - Will provide fast access to the data which the OdbcCommand will bring us.


The first example will focus on accessing our test data, and it needs to be printed only, and as long as we do not normally work with ASP.NET, it means that you have the most basic data access Example is to be shown. Since we write directly on the (top) page, we do not need to add any markup codes. Just go to CodeBehind (.cs) file, and add the following code to the page_load method:


try
{
    using(OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MySQLConnStr"].ConnectionString))
    {
        connection.Open();
        using(OdbcCommand command = new OdbcCommand("SELECT name FROM test_users", connection))
        using(OdbcDataReader dr = command.ExecuteReader())
        {
            while(dr.Read())
                Response.Write(dr["name"].ToString() + "<br />");
            dr.Close();
        }
        connection.Close();
    }
}
catch(Exception ex)
{
    Response.Write("An error occured: " + ex.Message);
}

Well, if you come from PHP world, then you are running due to the large amount of code needed to perform simple database extraction. However, C # is an object-oriented language, and sometimes it takes a bit more code to create a more elegant solution. Of course, the code can be gathered in re-usable classes and methods in this way, but for now, we do it in a basic way.

Now, let me try to explain the different lines of code. First we create an OdbcConnection object, to establish a connection with the database. We use Configuration Manager to obtain the connection string, which we had stored in the web.config file in the previous chapter. This is with a () build, which is a way to tell C #, when the block of code is over, then this object should be settled. If we do not use it, then we have to make a connection call instead. After we did this, we open the connection, and then we make an example of the ODBC compliant class, which is used to stop SQL queries against the database. Using the command object, we ask the database, and in return we get an ODBCdidder. Datacirder is the easiest and fastest way to access a database in .NET.

On the loop and on every trip through the datreader, we output the name field to the database, as well as with the linebreaker tag. Very simple, right? As you can see, the entire data access block has been explained in an attempt. Chalk Block. While chatting with a database, a lot of things may be wrong, so a try is recommended using the chalk block. Using Dr. ["name"], we get the result as an object, this is the reason I am calling ToString () if you wish to have more control over the data obtained from the reader, * Can use methods such as GetInt32 (), GetString () etc. However, for these methods you need to use the indicator of the field in your query, and not the name of the area, which is not convenient

Run the website, and see the output. It should have a bunch of names from our test table, or whatever table you are using